from django.shortcuts import render
import json

# Create your views here.

# 导入响应方法
from django.http import HttpResponse
from django.views import View


# Create your views here.
# 视图方法必须有一个接受参数request,接受请求报文的数据
def login(request):
    # 编写业务
    return HttpResponse('登录成功')


class RegisterView(View):
    def get(self, request):
        return HttpResponse('开始注册')

    def post(self, request):
        return HttpResponse('注册完成')


class QueryString(View):

    # 1.提取查询字符串数据
    # def get(self, request):
    #     name_info = request.GET.get('name', 'Kagari')
    #     # 获取相同key多个值查询字符串数据
    #     # name_info = request.GET.getlist('name', 'Kagari')
    #     age_info = request.GET.get('age')
    #     print(name_info + '\t' + age_info)
    #     return HttpResponse('GET查询字符串参数：%s-%s' % (name_info, age_info))

    # 3.URL路径参数：提取URL路径中的特定部分数据
    # 用默认转换器和自定义转换器匹配路径
    def get(self, request, name, mobile):
        print(name)
        print(mobile)
        return HttpResponse('用自定义转换器匹配到：%s-%s' % (name, mobile))

    # 2.提取请求体数据
    # 2.1 表单类型请求体数据(Form Data)
    # def post(self, request):
    #     name_info = request.POST.get('name', 'Takanashi')
    #     # 获取相同key多个值查询字符串数据
    #     # name_info = request.POST.getlist('name', 'Takanashi')
    #     age_info = request.POST.get('age')
    #     print(name_info + '\t' + age_info)
    #     return HttpResponse('POST查询字符串参数：%s-%s' % (name_info, age_info))

    # 2.2 非表单类型请求体数据(Non-Form Data)：JSON
    def post(self, request):
        data_str = request.body.decode()   # 转换后的数据是json字符串
        data_dict = json.loads(data_str)    # 讲json数据转换成字典
        name_info = data_dict.get('name')
        age_info = data_dict.get('age')
        print(name_info + '\t' + age_info)
        return HttpResponse('非表单类型请求体参数：%s--%s' % (name_info, age_info))


class RePathView(View):
    def get(self, request, mobile):
        print(mobile)
        return HttpResponse(mobile)